﻿using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.BaseData.Dto;
using NP.BPMReportPlatform.Common;
using NP.BPMReportPlatform.Common.Dtos;
using NP.BPMReportPlatform.Contract;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using static NP.BPMReportPlatform.Contract.Dtos.ContractEnum;

namespace NP.BPMReportPlatform.Common
{
    /// <summary>
    /// 公共接口
    /// </summary>
    //[Authorize]
    [ApiController]
    [Route("api/common")]
    public class CommonController : BPMReportPlatformController
    {
        private readonly ICommonService _commonService;


        public CommonController(ICommonService commonService)
        {
            _commonService = commonService;
        }

        /// <summary>
        /// 根据条件查询甲方
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("all-corporate")]
        public Task<PagedResultDto<ItemPairDto>> GetAllCorporateList([FromQuery] CommonSearchDto searchDto)
        {
            return _commonService.GetAllCorporateList(searchDto);
        }

        /// <summary>
        /// 根据条件查询城市数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("filter-city")]
        public Task<List<TreeNodes>> GetAllCities([FromQuery] string name)
        {
            return _commonService.GetAllCities(name);
        }

        /// <summary>
        /// 获取字典表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Route("GetDictionaries")]
        [HttpGet]
        public async Task<PagedResultDto<Dictionaries>> GetKeyValues([FromQuery] GetKeyValuesInput input)
        {
            return await _commonService.GetKeyValues(input);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="emailType">邮件类型</param>
        /// <param name="userName">人员姓名</param>
        /// <param name="workNumber">工号</param>
        /// <param name="contractCode">合同编码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("send-email")]
        public Task SendEmail([FromQuery] ContractEmailTypeEnum emailType, [FromQuery] string userName,
            [FromQuery] string workNumber, [FromQuery] string contractCode)
        {
            return _commonService.SendEmail(emailType, userName, workNumber, contractCode);
        }

        /// <summary>
        /// 立邦企业微信通用通知消息WebAPI接口
        /// </summary>
        /// <param name="messageDto">消息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("msgSendForApi")]
        public async Task MsgSendForAPI([FromBody] SendMessageDto messageDto)
        {
           await _commonService.MsgSendForAPI(messageDto.title,messageDto.message, messageDto.url,messageDto.users);
        }

        /// <summary>
        /// 通用 导出excel模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("ExportTemplate")]
        public async Task<FileResult> ExportTemplate([FromQuery] string fileName)
        {
            var rst = await _commonService.ExportTemplate(fileName);
            rst.Close();//资源释放
            return File(rst.ToArray(), "application/octet-stream", $"{fileName}.xlsx");
        }
    }
}
